import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-second-kill-time',
  templateUrl: './second-kill-time.component.html',
  styleUrls: ['./second-kill-time.component.scss'],
})
export class SecondKillTimeComponent implements OnInit {
  @Input() time: any;
  timer;
  remainingTime: any;
  remainingTimeNoShow: any;
  dayStr: any;
  hourStr: any;
  minStr: any;
  secStr: any;

  constructor(

  ) { }

  ngOnInit() {
    this.time = this.time.replace(/-/g, "/");
    const timer = setInterval(() => {

      const stopTime: Date = new Date(this.time);
      const nowTime: Date = new Date();
      const jianGe = (stopTime.valueOf() - nowTime.valueOf()) / 1000;
      let day = Math.floor(jianGe / 60 / 60 / 24);
      let hour = Math.floor(jianGe / 60 / 60 % 24);
      let min = Math.floor(jianGe / 60 % 60);
      let sec = Math.floor(jianGe % 60);
      if (jianGe <= 0 || isNaN(jianGe)) {
        day = 0;
        hour = 0;
        min = 0;
        sec = 0;
      }
      this.dayStr = day + '';
      this.secStr = "00";
      this.minStr = "00";
      this.hourStr = "00";
      if (sec < 10) {
        this.secStr = '0' + sec;
      } else {
        this.secStr = sec + "";
      }
      if (min < 10) {
        this.minStr = '0' + min;
      } else {
        this.minStr = '' + min;
      }
      if (hour < 10) {
        this.hourStr = '0' + hour;
      } else {
        this.hourStr = hour + "";
      }

      if (day > 0) {
        this.remainingTime = day + '天 ' + this.hourStr + ':' + this.minStr + ':' + this.secStr;
        if (!this.remainingTimeNoShow) {
          this.remainingTimeNoShow = this.remainingTime;
        }
      } else {
        this.remainingTime =  this.hourStr + ':' + this.minStr + ':' + this.secStr;
        if (!this.remainingTimeNoShow) {
          this.remainingTimeNoShow = this.remainingTime;
        }
      }

      if (day === 0 && hour === 0 && min === 0 && sec === 0) {
          // 关闭定时器
        clearInterval(this.timer);
      }
    }, 1000);
   }

}
